package parse;

import java.util.ArrayList;
import java.util.List;

import Const.Constant;

import com.sun.org.apache.xpath.internal.Expression;

public class SFW extends SQL{
	private Attribute_list attr_list;
	private TableName_list tbl_list;
	private Condition conditions;
	private Orderby order;
	
	public SFW(Attribute_list attr_l, TableName_list tbl_l, Condition conditions, Orderby o){
		this.attr_list = attr_l;
		this.tbl_list = tbl_l;
		this.conditions = conditions;
		this.order = o;
	}
	
	public Attribute_list GetAttrList(){
		return this.attr_list;
	}
	
	public TableName_list GetTblList(){
		return this.tbl_list;
	}

	public LQPlan CrtAgbPlan(){
		Pi pi = null;
		Sigma sigma = null;
		Algebra algebra = null;
		LQPlan plan = null;
		
		if(this.tbl_list.GetTNList().size() > 1){
			Descartes m = null, n = null;
			
			StringBuffer t1 = new StringBuffer(this.tbl_list.GetTNList().get(0).GetTableName());
			StringBuffer t2 = new StringBuffer(this.tbl_list.GetTNList().get(1).GetTableName());
			t1.setLength(Constant.MAXNAME);
			t2.setLength(Constant.MAXNAME);
			String s1 = t1.toString();
			String s2 = t2.toString();
			
			m = new Descartes(new AlgebraTable(s1), new AlgebraTable(s2));
			
			for(int i=0; i<this.tbl_list.GetTNList().size()-2; i++){
				t1 = new StringBuffer(this.tbl_list.GetTNList().get(i).GetTableName());
				t1.setLength(Constant.MAXNAME);
				s1 = t1.toString();
				
				n = new Descartes(m, new AlgebraTable(s1));
				m = n;
			}
			algebra = m;
		}
		else{
			algebra = new AlgebraTable(this.tbl_list.GetTNList().get(0).GetTableName());
		}
		
		if(conditions != null){
			sigma = new Sigma(conditions, algebra);
			pi = new Pi(attr_list, sigma);
		}
		else
			pi = new Pi(attr_list, algebra);
		
		plan = new LQPlan(pi);
		return plan;
	}
}